//
//  EMMemberInfoViewController.m
//  ElectronicMall
//
//  Created by 史伟文 on 14/12/25.
//  Copyright (c) 2014年 cczu. All rights reserved.
//

#import "EMMemberInfoViewController.h"
#import "UINavigationController+EMNavi.h"
#import "EMSetQuestionViewController.h"
#import "EMPasswordModViewController.h"
#import "EMAddAddressViewController.h"
#import "MBProgressHUD.h"
#import "EMCityListReqBody.h"
#import "EMCityListRespBody.h"
#import "AppDelegate.h"
#import "EMModifyInfoViewController.h"
#import "EMAddressMgrViewController.h"
#import "EMGetScoreReqBody.h"
#import "EMGetScoreRespBody.h"
#import "EMGetMemberInfoReqBody.h"
#import "EMGetMemberInfoRespBody.h"
#import "UserInfo.h"

@interface EMMemberInfoViewController ()
{
    AppDelegate *app;
    NSString *score;
    EMGetMemberInfoRespBody *memberInfo;
    UITableView *infoTable;
}
@end

@implementation EMMemberInfoViewController
- (void)hideBottomBar
{
    for(UIView *view in self.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
            view.hidden = YES;
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self hideBottomBar];
    score = [[NSString alloc] init];
    memberInfo = [[EMGetMemberInfoRespBody alloc] init];
    [self.navigationController setNavigationMode:EMNavigationModeDefault andSubMode:0];
    self.navigationItem.title = @"个人信息";
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 30, 32);
    [button setImage:[UIImage imageNamed:@"button_导航_返回_白"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:button];
    
    [self getUserScore];
    [self getMemberInfo];
    [infoTable reloadData];
}
- (void)backAction: (UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    infoTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    infoTable.delegate = self;
    infoTable.dataSource = self;
    infoTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    infoTable.backgroundColor = RGB(242, 242, 242);
    [self.view addSubview:infoTable];
}

- (void)modifyMemberInfo:(UIButton *)sender
{
    EMModifyInfoViewController *controller = [[EMModifyInfoViewController alloc] init];
    controller.memberInfo = memberInfo;
    [self.navigationController pushViewController:controller animated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 6;
    } else if (section == 1) {
        return 1;
    } else if (section == 2) {
        return 3;
    } else {
        return 4;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"infoCell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
//    while ([cell.subviews lastObject] == nil) {
//        [(UIView *)[cell.subviews lastObject] removeFromSuperview];
//    }
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {
                cell.textLabel.text = @"个人信息";
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
                UIButton *editInfoButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [editInfoButton setImage:[UIImage imageNamed:@"icon_个人信息_修改"] forState:UIControlStateNormal];
                editInfoButton.frame = CGRectMake(W(260), H(2.5), W(35), H(35));
                [editInfoButton addTarget:self action:@selector(modifyMemberInfo:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:editInfoButton];
                break;
            }
            case 1:
            {
                cell.textLabel.text = [NSString stringWithFormat:@"性别：%@", [memberInfo.SEX isEqualToString:@"M"] ? @"男" : @"女"];
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
                break;
            }
            case 2:
            {
                cell.textLabel.text = [NSString stringWithFormat:@"生日：%@", memberInfo.BIRRHDAY];
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
                break;
            }

            case 3:
            {
                cell.textLabel.text = [NSString stringWithFormat:@"QQ号：%@", memberInfo.QQ];
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
                break;
            }
            case 4:
            {
                cell.textLabel.text = [NSString stringWithFormat:@"邮箱：%@", memberInfo.EMAIL];
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
                break;
            }
            case 5:
            {
                cell.textLabel.text = [NSString stringWithFormat:@"喜好：%@", memberInfo.LOVE];
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
                break;
            }
            default:
                break;
        }
    } else if (indexPath.section == 1) {
        cell.textLabel.text = @"地址管理";
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
        UIButton *gotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [gotoButton setImage:[UIImage imageNamed:@"icon_个人信息_向后"] forState:UIControlStateNormal];
        gotoButton.frame = CGRectMake(W(260), H(7.5), W(30), H(30));
        [cell.textLabel addSubview:gotoButton];
    } else if (indexPath.section == 2) {
        switch (indexPath.row) {
            case 0:
            {
                cell.textLabel.text = @"账号安全";
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
                
                break;
            }
            case 1:
            {
                cell.textLabel.text = @"密码修改";
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
                UIButton *gotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [gotoButton setImage:[UIImage imageNamed:@"icon_个人信息_向后"] forState:UIControlStateNormal];
                gotoButton.frame = CGRectMake(W(260), 0, W(30), H(30));
                [cell.textLabel addSubview:gotoButton];
                UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(W(5), H(29), W(310), 1)];
                lineView.backgroundColor = RGB(222, 222, 222);
                [cell addSubview:lineView];
                break;
            }
            case 2:
            {
                cell.textLabel.text = @"设置安全问题";
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
                UIButton *gotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [gotoButton setImage:[UIImage imageNamed:@"icon_个人信息_向后"] forState:UIControlStateNormal];
                gotoButton.frame = CGRectMake(W(260), 0, W(30), H(30));
                [cell.textLabel addSubview:gotoButton];
                break;
            }
                
            default:
                break;
        }
    } else {
        switch (indexPath.row) {
            case 0:
            {
                cell.textLabel.text = @"我的积分";
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
                break;
            }
            case 1:
            {
                cell.textLabel.text = [NSString stringWithFormat:@"剩余积分：%@", score];
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
                UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(W(5), H(29), W(310), 1)];
                lineView.backgroundColor = RGB(222, 222, 222);
                [cell addSubview:lineView];
                break;
            }
            case 2:
            {
                cell.textLabel.text = @"获取积分方式";
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
                break;
            }
            case 3:
            {
                cell.textLabel.text = @"1.购买获得\n2.赠送获得\n3.参加促销活动获得";
                cell.textLabel.numberOfLines = 0;
                cell.textLabel.textColor = [UIColor grayColor];
                cell.textLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
                break;
            }
            default:
                break;
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return H(40);
        } else {
            return H(25);
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return H(40);
        } else {
            return H(25);
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            return H(40);
        } else {
            return H(30);
        }
    } else {
        if (indexPath.row == 0 || indexPath.row == 2) {
            return H(40);
        } else if (indexPath.row == 1){
            return H(25);
        } else {
            return H(50);
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return H(10);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        EMAddressMgrViewController *controller = [[EMAddressMgrViewController alloc] init];
        [self.navigationController pushViewController:controller animated:YES];
    } else if (indexPath.section == 2) {
        if (indexPath.row == 1) {
            EMPasswordModViewController *controller = [[EMPasswordModViewController alloc] init];
            [self.navigationController pushViewController:controller animated:YES];
        } else if (indexPath.row == 2) {
            EMSetQuestionViewController *controller = [[EMSetQuestionViewController alloc] init];
            [self.navigationController pushViewController:controller animated:YES];
        }
    }
}

- (void)getMemberInfo
{
    [MBProgressHUD  showHUDAddedTo:self.navigationController.view animated:YES];
    EMGetMemberInfoReqBody *reqBody = [[EMGetMemberInfoReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"SELECT-USER-INFO";
    reqBody.USERID = [UserInfo sharedUserInfo].m_userName;
    reqBody.SESSION = [UserInfo sharedUserInfo].m_session;
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        EMGetMemberInfoRespBody *respBody = [EMGetMemberInfoRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            memberInfo = respBody;
        } else {
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];
}

- (void)getUserScore
{
    [MBProgressHUD  showHUDAddedTo:self.navigationController.view animated:YES];
    EMGetScoreReqBody *reqBody = [[EMGetScoreReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"USER-GET-SCORE";
    reqBody.USERID = [UserInfo sharedUserInfo].m_userName;
    reqBody.SESSION = [UserInfo sharedUserInfo].m_session;
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        EMGetScoreRespBody *respBody = [EMGetScoreRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            score = respBody.SCORE;
        } else {
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];
}
@end
